import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
import flash.ui.Keyboard;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.filters.GlowFilter;
import haxe.FastList; 
import flash.display.Shape;
import flash.events.MouseEvent;

import flash.utils.Timer;
import flash.events.TimerEvent;

//import haxe.Timer;
import flash.events.Event;


import Images;  
import Blob;
import Character;

class FinalScene extends Scene
{
  var time:Int;
  var PostSceneStep:Int ;
  var fadeStart:Float;
  var square:Sprite;

  public function new(inBackground:BitmapData,inText:FastList<TextArea>,inCharacters:FastList<Character>)
  {
     super(inBackground,inText,inCharacters);
     time = 0;
     PostSceneStep = 0;
  }
  
//
   public override function checkWin() : Bool
    {
     
      var win:Bool = false;
      time++;
      if(time > 1000)
         { 
            win = true;
         }
      return win;
    }

  public override function timerHandler(event:TimerEvent) {
    if(State == "CutScence")
    {
       PreScene();
    }
    else if(State == "Wait")
    {
    }
    else if(State == "Playing")
    {
    MovementPhase();
    }
    else if(State == "Victory")
    {
      PostScene();
    }    
    else if (State == "Gave Up")
    {
      PostScene();
    }
}

public function PostScene()
{
   if(PauseTillKeyPressed)
     {
      return;
     }
   if(PostSceneStep == 0)
     {
    for (character in characters.iterator()) 
      {
      if(character.characterType == "Main")
	{
	character.attacking = 0;
        character.CurrentCharacter = cast(character,MainCharacter).kneel;
        clearCharacters();
	drawCharacters();
	}
      }
       label1 = new TextField();
        label1.x = 0; label1.y = 0;
        label1.width = 640; label1.height = 100;
        addChild(label1);
        label1.htmlText = "<font size='25'>...</font>";
        label1.autoSize = TextFieldAutoSize.CENTER;
        PauseTillKeyPressed = true;
	PostSceneStep++;
     }
   else if(PostSceneStep == 1)
    {
    for (character in characters.iterator()) 
      {
      if(character.characterType == "Main")
	{
	character.attacking = 0;
        clearCharacters();
        character.CurrentCharacter = cast(character,MainCharacter).lying;
        character.postion =  new Point(character.postion.x, 426 - character.CurrentCharacter.mRect.height);
	drawCharacters();
	}
      }

     if(victory == "GaveUp")
       {
         label1.htmlText = "<font size='25'>I Give up. You're right, I'm just being stuborn</font>";
       }
      else
       {
        label1.htmlText = "<font size='25'>I don't want to continue. You win. At least, it's finally over.</font>";
       }
        PauseTillKeyPressed = true;
	PostSceneStep++;
        square = new Sprite();
        addChild(square);
        fadeStart = 0.0;
    }
   else if(PostSceneStep == 2)
    {

      DrawFade();
      fadeStart = fadeStart + 0.05;
      if(fadeStart >= 1)
      {  
       if(victory != "GaveUp")
       { 
         removeChild(label1);
         addChild(label1);
         label1.htmlText = "<font size='25' color='#ffffff'>I gave up, would you have done the same?</font>";
       }
      PauseTillKeyPressed = true;  
      PostSceneStep++;
      }
    }
    else if(PostSceneStep == 3)
     {
       time = 0;
      PostSceneStep = 0;
       unLoad();
     }
   //Collapse
   //fade to back
   //Text if you didn't give up.
}

private function DrawFade()
{

      
      //square.graphics.lineStyle(3,0x00ff00);
      square.graphics.beginFill(0x000000,fadeStart);
      square.graphics.drawRect(0,0,640,426);
      square.graphics.endFill();
      square.x = 0;
      square.y = 0;
}

private override function onYesButtonClick(event:MouseEvent) {
victory = "GaveUp";
if(label1 != null)
   {removeChild(label1);}
 removeChild(YesButton);
 removeChild(NOButton);
State = "Victory";

}

private override function onNOButtonClick(event:MouseEvent) {
removeChild(label1);
removeChild(YesButton);
removeChild(NOButton);
State = "Playing";
}

}